package com.zhonghui.kf.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zhonghui.kf.util.ExcelcellsUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhonghui.common.annotation.Log;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.core.domain.AjaxResult;
import com.zhonghui.common.enums.BusinessType;
import com.zhonghui.kf.domain.ProcurementAnswer;
import com.zhonghui.kf.service.IProcurementAnswerService;
import com.zhonghui.common.utils.poi.ExcelUtil;
import com.zhonghui.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客服问答Controller
 * 
 * @author y
 * @date 2023-11-29
 */
@RestController
@RequestMapping("/chat/answer")
public class ProcurementAnswerController extends BaseController
{
    @Autowired
    private IProcurementAnswerService procurementAnswerService;


    /**
     * 查询客服问答列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProcurementAnswer procurementAnswer)
    {
        startPage();
        List<ProcurementAnswer> list = procurementAnswerService.selectProcurementAnswerList(procurementAnswer);
        return getDataTable(list);
    }

    /**
     * 导出客服问答列表
     */
    @Log(title = "客服问答", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProcurementAnswer procurementAnswer)
    {
        List<ProcurementAnswer> list = procurementAnswerService.selectProcurementAnswerList(procurementAnswer);
        ExcelUtil<ProcurementAnswer> util = new ExcelUtil<ProcurementAnswer>(ProcurementAnswer.class);
        util.exportExcel(response, list, "客服问答数据");
    }

    /**
     * 获取客服问答详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(procurementAnswerService.selectProcurementAnswerById(id));
    }

    @PostMapping(value = "/import")
    public AjaxResult getInfo(MultipartFile file) throws Exception {
        if (ObjectUtils.isEmpty(file) || file.getSize() <= 0) {
            return AjaxResult.error("上传文件大小为空");
        }
        String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        Workbook hssfWorkbook = null;
        if (fileSuffix.equals(".xlsx")){
            hssfWorkbook =  ExcelcellsUtil.cell(file.getInputStream());;//Excel 2007
        }else if (fileSuffix.equals(".xls")){
            hssfWorkbook =  ExcelcellsUtil.cell(file.getInputStream());;//Excel 2003
        }
        ProcurementAnswer procurementAnswer = null;
        List<ProcurementAnswer> list = new ArrayList<ProcurementAnswer>();
        // 循环工作表Sheet
        for (int numSheet = 0; numSheet <hssfWorkbook.getNumberOfSheets(); numSheet++) {
            //HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            // 循环行Row
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                Row hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow != null) {
                     procurementAnswer = new ProcurementAnswer();
                    procurementAnswer.setAnswer(hssfRow.getCell(2).toString());
                    procurementAnswer.setAsk(hssfRow.getCell(1).toString());
                    list.add(procurementAnswer);
                }
            }
        }

        procurementAnswerService.insertBatch(list);

        return AjaxResult.success();
    }

    /**
     * 新增客服问答
     */
    @Log(title = "客服问答", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProcurementAnswer procurementAnswer)
    {
        return toAjax(procurementAnswerService.insertProcurementAnswer(procurementAnswer));
    }

    /**
     * 修改客服问答
     */
    @Log(title = "客服问答", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProcurementAnswer procurementAnswer)
    {
        return toAjax(procurementAnswerService.updateProcurementAnswer(procurementAnswer));
    }

    /**
     * 删除客服问答
     */
    @Log(title = "客服问答", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(procurementAnswerService.deleteProcurementAnswerByIds(ids));
    }
}
